/*
 * @features: 功能
 * @description: 说明
 * @Date: 2021-09-30 23:49:57
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-03 22:22:38
 * @LastEditors: judu233
 */
import { _decorator, error, Toggle } from "cc";
import { ProxyOb } from "./Core/ProxyOb";
import VmBase from "./Core/VmBase";

const { ccclass, property, menu } = _decorator;

@ccclass
@menu('VM/VM-Toggle')
export default class VmToggle extends VmBase {
    /**代理对象 */
    proxy: ProxyOb<Toggle>['proxy'];

    onDataInit() {
        try {
            if (this.watch.controller) {
                this.node.on(Toggle.EventType.TOGGLE, this.chooseChange, this);
                this.proxyOb.isCheckValue = false;
            }
            this.setComponentValue( ProxyOb.getValue(this.watch.path, false));
        } catch (err) {
            error(`节点：${this.node.name}组件的VmProgress组件init失败`);
            error(err);
        }
    }

    setComponentValue(value: any) {
        this.watch._watchComponent[this.watch.componentProperty] = value;
    }

    chooseChange(toggle: Toggle) {
        this.proxy.isChecked = toggle.isChecked;
    }
}
